<template>
  <view class="hfzMain">
	  <u-sticky :enable="enable">
	   	<view class="myTerminalSearch solid-bottom">
	   		<u-search class="myTerminalSearchInput" placeholder="请输入代理商号或名称搜索" bg-color="#fff" shape="square" :show-action="true" v-model="sn_code" :clearabled="true" @search="inputChange" @custom="inputChange"></u-search>
	   	</view>
	  </u-sticky>
    <view class="hzfBox">
      <view v-if="list.length > 0">
        <view
          class="hzfItem"
          v-for="(item, index) in list"
          :key="index"
          @tap="goToDetail(item.nick_name, item.id)"
        >
          <view class="real_name">{{ item.nick_name?item.nick_name:'-----' }}</view>
          <view class="agent_code flex align-center justify-between"
            ><text>代理商号：{{ item.agent_code?item.agent_code:'-----' }}</text
            ><text class="tag_name">{{ $u.timeFormat(item.create_time, 'yyyy-mm-dd hh:MM:ss') }}</text></view
          >
        </view>
      </view>
      <!-- 暂无数据 -->
      <view class="noMachine" v-if="list.length == 0 && isLoad">
        <image
          src="../../static/images/noData/zwjl.png"
          mode="widthFix"
        ></image>
        <view style="color: #c0c4cc">暂无数据</view>
      </view>
    </view>
    <u-loadmore
      :status="status"
      @loadmore="loadmore"
      style="padding: 0 0 50rpx"
      v-if="list.length > 0"
    />
    <u-back-top :scroll-top="scrollTop"></u-back-top>
    <toast></toast>
  </view>
</template>
<script>
export default {
  data() {
    return {
      scrollTop: 0,
      cur_page: 1,
      pageNumber: 20,
      list: [],
      noMore: false,
      status: "loadmore",
      isLoad: false,
	  
	  sn_code:''
    };
  },
  onLoad(options) {
    this.type = options.type;
    this.getListData();
  },
  onShow() {
  		this.enable = true;
  },
  onHide() {
  		this.enable = false;
  },
  onPageScroll(e) {
    this.scrollTop = e.scrollTop;
  },
  // 点击下载更多
  loadmore() {
    if (this.noMore) {
      this.status = "nomore";
    } else {
      this.cur_page = ++this.cur_page;
      this.status = "loadmore";
      this.getListData();
    }
  },
  // 下拉触底
  onReachBottom() {
    if (this.noMore) {
      this.status = "nomore";
    } else {
      this.status = "loading";
      this.cur_page = ++this.cur_page;
      this.getListData();
    }
  },
  onPullDownRefresh() {
    this.list = [];
    this.cur_page = 1;
    this.sn_code = "";
    this.noMore = false;
    this.isLoad = false;
    this.status = "loadmore";
    this.getListData();
  },
  methods: {
    getListData() {
      this.$u.api
        .myAgentGroup({
          cur_page: this.cur_page,
          pageNumber: this.pageNumber,
          type: this.type,
		  key_words: this.sn_code
        })
        .then((res) => {
          if (res.status == 200) {
            var datas = res.result.BJ;
            if (this.cur_page == 1 && datas.length == 0) {
              this.noMore = true;
              this.status = "nomore";
            }
            if (this.cur_page == 1 && datas.length < this.pageNumber) {
              this.noMore = true;
              this.status = "nomore";
            }
            if (this.cur_page > 1 && datas.length < this.pageNumber) {
              this.noMore = true;
              this.status = "nomore";
            }
            if (this.cur_page > 1 && datas.length == this.pageNumber) {
              this.status = "loading";
            }
            this.list = this.list.concat(datas);
          }
          this.isLoad = true;
          uni.stopPullDownRefresh();
        })
		.catch((res) => {
			if (res.errMsg == "request:fail timeout") {
				this.$u.toast("请求超时，请重试!");
			} else if (res.errMsg == "request:fail") {
				this.$u.toast("请求失败，请重试");
			} else {
				 this.$u.toast("请求失败，请重试");
			}
			this.$loading(false);
			setTimeout(() => {
				this.$u.route({
					type: "navigateBack",
					delta: 1,
				});
			}, 2000);
		});
    },
    goToDetail(name, id) {
      this.$u.route({
        url: "/pages/inventoryManagement/detail",
        params: {
          name: name,
          agent_id: id,
          type:this.type
        },
      });
    },
	
	// 搜索
	inputChange(){
		this.list = [];
		this.cur_page = 1;
		this.getListData();
	},
  },
};
</script>
<style lang="scss">
page {
  background: #fff;
}
.hzfBox {
  padding: 30rpx;
  .hzfItem {
    padding: 30rpx;
    box-shadow: 0 0 10rpx #ccc;
    margin-bottom: 30rpx;
    border-radius: 10rpx;
    .real_name {
      font-size: 30rpx;
      color: #333;
      font-weight: bold;
    }
    .agent_code {
      margin-top: 10rpx;
	  text{
		  font-size: 26rpx;
	  }
      .tag_name {
        color: #999;
      }
    }
  }
}
.myTerminalSearch {
  padding: 0 30rpx;
  background-color: #fff;
  height: 80rpx;
  width: 100%;
  overflow: hidden;
  box-sizing: border-box;

  .myTerminalSearchInput {
    height: 80upx;
    width:96%;
    float: left;
  }
}
</style>